<template>
  <div style="padding: 10px">
    <!--    模糊检索搜索区域-->
    <el-form :inline="true" class="demo-form-inline" style="margin: 10px 0">
      <el-form-item label="路径">
        <el-input
          type="text"
          placeholder="请输入路径"
          v-model="permission.path"
        ></el-input>
      </el-form-item>
      <el-form-item label="名称">
        <el-input
          type="text"
          placeholder="请输入名称"
          v-model="permission.permissionName"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          type="text"
          placeholder="请输入备注"
          v-model="permission.permissionRealName"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="load" type="primary">查询</el-button>
        <el-button @click="add" type="primary">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      stripe
      style="width: 100%; height: 500px; overflow: auto"
    >
      <el-table-column
        prop="permissionId"
        label="编号"
        sortable
        width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="permissionName" align="center" label="名称">
      </el-table-column>
      <el-table-column prop="path" label="路径" align="center">
      </el-table-column>
      <el-table-column prop="permissionRealName" label="备注" align="center">
      </el-table-column>
      <el-table-column prop="permissionIcon" label="图标" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete(scope.row.permissionId)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog :title="title" v-model="dialogVisible" width="30%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="名称">
          <el-input v-model="form.permissionName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="form.path" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.permissionRealName"
            style="width: 80%"
          ></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="form.permissionIcon" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Permission",
  components: {},
  data() {
    return {
      loading: true,
      form: {},
      dialogVisible: false,
      permission: {},
      pageNum: 1,
      pageSize: 6,
      total: 0,
      tableData: [],
      title: "", //修改、增加标题名
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.loading = true;
      request
        .post(
          "/permission/page?pageNum=" +
            this.pageNum +
            "&pageSize=" +
            this.pageSize,
          JSON.stringify(this.permission)
        )
        .then((res) => {
          this.loading = false;
          this.tableData = res.data.list;
          this.total = res.data.total;
        });
    },
    add() {
      this.title = "添加权限";
      this.dialogVisible = true;
      this.form = {};
    },
    save() {
      if (this.form.permissionId) {
        // 更新
        request.put("/permission/", this.form).then((res) => {
          if (res.code === 0) {
            this.$message({
              type: "success",
              message: "更新成功",
            });
          }
          this.load(); // 刷新表格的数据
          this.dialogVisible = false; // 关闭弹窗
        });
      } else {
        // 新增

        request.post("/permission/", this.form).then((res) => {
          if (res.code === 0) {
            this.$message({
              type: "success",
              message: "新增成功",
            });
          }

          this.load(); // 刷新表格的数据
          this.dialogVisible = false; // 关闭弹窗
        });
      }
    },
    handleEdit(row) {
      this.title = "修改权限";
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    handleDelete(permissionId) {
      request.delete("/permission/" + permissionId).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: "success",
            message: "删除成功",
          });
        }
        this.load(); // 删除之后重新加载表格的数据
      });
    },
    handleSizeChange(pageSize) {
      // 改变当前每页的个数触发
      this.pageSize = pageSize;
      this.pageNum = 1;
      this.load();
    },
    handleCurrentChange(page) {
      // 改变当前页码触发
      this.pageNum = page;
      this.load();
    },
  },
};
</script>
